using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Configuration;
using AppConfiguration;

namespace XRMServiceLibrary
{
    public class SendSMTPMailer
    {
        String mailFrom;
        private static string mailServer;
        private static int mPort;

        public void SentMail(String mailTo, String subject, String body, String strattachment,
                             String strattachmentname, String MailCC, String MailBcc)
        {
            ConfigReader objConfiguration = new ConfigReader();
            mailFrom = objConfiguration.MailToUser;
            mailServer = "";
            mPort = 0;
            // Convert.ToInt32(objConfiguration.SMTPPort);

            if (!string.IsNullOrEmpty(mailTo))
            {
                MailMessage objMessage = new MailMessage(mailFrom, mailTo, subject, body);
                //Instantiating the SmtpClient
                SmtpClient objSmtpClient = new SmtpClient(mailServer, mPort);

                if (!String.IsNullOrEmpty(MailCC))
                {
                    //for CC
                    if (MailCC != "" || MailCC != String.Empty)
                    {
                        string[] mailids = MailCC.Split(new char[] { ',' });
                        foreach (string mailid in mailids)
                        {
                            if (mailid.ToLower() == mailTo.ToLower()) { }
                            else
                            {
                                if ((!objMessage.CC.Contains(new MailAddress(mailid))))
                                {
                                    objMessage.CC.Add(new MailAddress(mailid));
                                }
                            }
                        }
                    }

                }


                if (!String.IsNullOrEmpty(strattachment))
                {
                    if (!strattachment.Contains(".doc"))
                    {
                        byte[] data1 = Encoding.ASCII.GetBytes((strattachment));
                        MemoryStream ms = new MemoryStream(data1);
                        Attachment data = new Attachment(ms, strattachmentname, "text/html");
                        objMessage.Attachments.Add(data);
                    }
                    else
                    {

                        Attachment data = new Attachment(strattachment);
                        objMessage.Attachments.Add(data);
                    }
                }

                if (!String.IsNullOrEmpty(MailBcc))
                {
                    //Add Developer as BCC
                    objMessage.Bcc.Add(new MailAddress(MailBcc));
                }
                //Enabling HTML formating
                objMessage.IsBodyHtml = true;

                //Setting default credentials to true
                objSmtpClient.UseDefaultCredentials = true;

                //Sending the mail. 
                try
                {
                    objSmtpClient.Send(objMessage);
                }
                catch (Exception ex)
                {
                    //Remain Silent
                    throw ex;
                }

                objMessage.Dispose();
                objSmtpClient = null;
                strattachment = null;
                body = null;
            }
        }
    }
}
